//
//  NetworkTools.swift
//  liveDemo
//
//  Created by satoshi_uma on 2020/10/22.
//  Copyright © 2020 satoshi_uma. All rights reserved.
//

import Alamofire
import UIKit

enum MethodType {
    case get
    case post
}

class NetworkTools {
    /// 网络请求封装
    /// - Parameters:
    ///   - type: 类型 GET POST
    ///   - URL: 网址
    ///   - parameter: 参数
    ///   - finishCallback: 回调
    class func requestData(type: MethodType,
                           URL: String,
                           parameter: [String: NSString]! = nil,
                           finishCallback: @escaping (_ result: Any, _ error:Any) -> Void){
        
        let method = type == .get ? (HTTPMethod.get) :  (HTTPMethod.post)
        
        Alamofire.request(URL, method: method, parameters: parameter, encoding: URLEncoding.default, headers: nil).responseJSON { response in

            let error = response.result.error
            
            guard let result = response.result.value else { return }
             
            finishCallback(result , error ?? "no error")

        }
    }
}
